﻿using System;
using System.Collections.Generic;
using System.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;

namespace NPMonitor.Utility
{
    public static class WebDriverHelper
    {
        public static int DefaultTimeoutSecs;
        public static IWebDriver WebDriver;

        public static IWebElement GetByClassName(ISearchContext webDriver, string className)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElement(By.ClassName(className));
               }
              );
            return webElement;
        }

        public static IWebElement GetById(ISearchContext webDriver, string id)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElement(By.Id(id));
               }
              );
            return webElement;
        }

        public static IWebElement GetByLinkText(ISearchContext webDriver, string linkText)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElement(By.LinkText(linkText));
               }
              );
            return webElement;
        }

        public static IWebElement GetByXPath(ISearchContext webDriver, string xPath)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElement(By.XPath(xPath));
               }
              );
            return webElement;
        }

        public static IWebElement GetByTagName(ISearchContext webDriver, string tagName)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElement(By.TagName(tagName));
               }
              );
            return webElement;
        }

        public static IWebElement GetByName(ISearchContext webDriver, string name)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElement(By.Name(name));
               }
              );
            return webElement;
        }

        public static List<IWebElement> GetWebElementsByLinkText(ISearchContext webDriver, string linkText)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElements(By.LinkText(linkText)).ToList();
               }
              );
            return webElement;
        }

        public static List<IWebElement> GetWebElementsById(ISearchContext webDriver, string id)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElements(By.Id(id)).ToList();
               }
              );
            return webElement;
        }

        public static List<IWebElement> GetWebElementsByName(ISearchContext webDriver, string id)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElements(By.Name(id)).ToList();
               }
              );
            return webElement;
        }

        public static List<IWebElement> GetWebElementsByClassName(ISearchContext webDriver, string className)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElements(By.ClassName(className)).ToList();
               }
              );
            return webElement;
        }

        public static List<IWebElement> GetWebElementsByTagName(ISearchContext webDriver, string tagName)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElements(By.TagName(tagName)).ToList();
               }
              );
            return webElement;
        }

        public static List<IWebElement> GetWebElementsByXPath(ISearchContext webDriver, string xPath)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElements(By.XPath(xPath)).ToList();
               }
              );
            return webElement;
        }

        public static IWebElement GetByCssSelector(ISearchContext webDriver, string cssSelector)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElement(By.CssSelector(cssSelector));
               }
              );
            return webElement;
        }

        public static List<IWebElement> GetWebElementsByCssSelector(ISearchContext webDriver, string cssSelector)
        {
            WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(DefaultTimeoutSecs));
            var webElement = wait.Until(
               d =>
               {
                   return webDriver.FindElements(By.CssSelector(cssSelector)).ToList();
               }
              );
            return webElement;
        }

        public static void DoubleClick(IWebDriver webDriver, IWebElement element)
        {
            Actions builder = new Actions(webDriver);
            builder.DoubleClick(element);
            builder.Build().Perform();
        }
        public static void MoveToElement(IWebDriver webDriver, IWebElement element)
        {
            Actions builder = new Actions(webDriver);
            builder.MoveToElement(element);
            builder.Build().Perform();
        }

        public static void ForceQuitWebBrowser(string browserType)
        {
            string processName = BrowserType.GetWebBrowserProcessName(browserType);

            if (!String.IsNullOrEmpty(processName))
            {
                Helpers.Utils.KillProcess(processName);
            }
        }

        public static void ForceQuitWebDriver(string browserType)
        {
            ForceQuitWebBrowser(browserType);

            string processName = BrowserType.GetWebDriverProcessName(browserType);

            if (!String.IsNullOrEmpty(processName))
            {
                Helpers.Utils.KillProcess(processName);
            }
        }
    }
}
